package com.jkoss.wine.shop.controller;

import static org.hamcrest.CoreMatchers.is;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.Constant;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.shop.entity.Shop;
import com.jkoss.wine.shop.entity.ShopFollow;
import com.jkoss.wine.shop.service.IShopFollowService;
import com.jkoss.wine.shop.service.IShopService;
import com.jkoss.wine.shop.vo.ShopVo;
import com.jkoss.wine.shop.vo.shopFollowVo;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.entity.Role;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

/**
 * 客户跟进记录 前端控制器
 * 
 * @Author 周华军
 * @Version 1.0, 2018-10-07
 * @See
 * @Since com.jkoss.wine.shop.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/shop/shopFollow")
public class ShopFollowController extends BaseController {

	@Autowired
	private IShopFollowService iShopFollowService;
	@Autowired
	private IShopService iShopService;

	/**
	 * 列表
	 * 
	 * @param shopName
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */

	@RequestMapping("/list")
	@RequiresPermissions("/shop/shopFollow/list")
	public String list(String shopId, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		// 构建排序条件 构建排序条件 是否删除：0-未删除、1-已删除、2-不能删除
		Wrapper wrapper = Condition.create().ne("sf.del", 1).eq("s.shop_id", shopId);
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("sf.add_time", false);
		}
		// Page resultPage = iShopFollowService.selectVoByPage(dwzPageBean.toPage(),
		// wrapper);
//		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		List list = iShopFollowService.selectVoByList(wrapper);
		map.put("shopFollow", list);
		Shop shop = iShopService.selectVoById(shopId);
		map.put("shop", shop);
		return "shop/shopFollow/list";
	}

	/**
	 * 去新增
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/shop/shopFollow/toInsert")
	public String toInsert(String shopId, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().ne("del", 1).eq("shop_state", 1);
		List<Shop> shops = iShopService.selectList(wrapper);
		map.put("shops", shops);
		map.put("shopId", shopId);
		return "shop/shopFollow/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/shop/shopFollow/toInsert")
	@ResponseBody
	public Object insert(@Valid ShopFollow shop_follow, HttpServletRequest request, HttpServletResponse response) {

		// // 判断客户是否存在
		// Wrapper wrapper = Condition.create().eq("spid", shop_follow.getSpid());
		// ShopFollow shopFollow = iShopFollowService.selectOne(wrapper);
		// if (!CommonUtil.isBlank(shopFollow) && shopFollow.getDel() == 0) {
		// return ajaxError("客户已存在");
		// }
		/*
		 * if (!CommonUtil.isBlank(shopFollow) && shopFollow.getDel() == 1) {
		 * shopFollow.setDel(0); shopFollow.setAddTime(CommonUtil.getDate());
		 * MerchantEmployees employees = (MerchantEmployees)
		 * request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		 * shopFollow.setOperator(employees.getEmployeeId()); if
		 * (iShopFollowService.updateById(shopFollow)) { return ajaxSuccess(); } else {
		 * return ajaxError(); } }
		 */
		// 添加时间
		shop_follow.setAddTime(CommonUtil.getDate());
		// 获取当前操作人
		MerchantEmployees employees = (MerchantEmployees) request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		shop_follow.setOperator(employees.getEmployeeName());
		// 是否删除：0-未删除、1-已删除、2-不能删除
		shop_follow.setDel(0);
		// 跟进人
		shop_follow.setMaid(getUserId());
		// 跟进时间
		shop_follow.setFwtm(CommonUtil.getDate());
		if (iShopFollowService.insert(shop_follow)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/shop/shopFollow/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().ne("del", 1).eq("shop_state", 1);
		// ShopFollow shopFollows = iShopFollowService.selectById(id);
		List<Shop> shops = iShopService.selectList(wrapper);
		// map.put("record", shopFollows);
		map.put("shops", shops);
		map.put("record", iShopFollowService.selectById(id));
		return "shop/shopFollow/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/shop/shopFollow/toUpdate")
	@ResponseBody
	public Object update(@Valid ShopFollow shop_follow, HttpServletRequest request, HttpServletResponse response) {
		// // 判断客户是否存在
		// Wrapper wrapper = Condition.create().eq("spid", shop_follow.getSpid());
		// ShopFollow shopFollow = iShopFollowService.selectOne(wrapper);
		// if (!CommonUtil.isBlank(shopFollow)) {
		// return ajaxError("客户已存在");
		// }
		// Wrapper wrapper = Condition.create().eq("spid", shop_follow.getSpid());
		// ShopFollow shopFollow = iShopFollowService.selectOne(wrapper);
		// if (!CommonUtil.isBlank(shopFollow)) {
		// return ajaxError("客户已存在");
		// }
		if (iShopFollowService.updateById(shop_follow)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/shop/shopFollow/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List list = new ArrayList<>();
		for (String string : id) {
			ShopFollow shopFollow = new ShopFollow();
			shopFollow.setId(string);
			// 是否删除 0-未删除，1-已删除
			shopFollow.setDel(1);
			list.add(shopFollow);
		}
		if (iShopFollowService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 查询客户名称
	 * 
	 * @param id
	 * @param shop
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getShopName")
	@ResponseBody
	public Object getShopName(String shopName, String spid, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("shop_Name", shopName).ne("del", 1).eq("shop_state", 1);
		List<Shop> list = iShopService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	/**
	 * 导出Excel
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/shopFollowexport")
	@RequiresPermissions("/shop/shopFollow/shopFollowexport")
	public String shopFollowexport(ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		/**
		 * @param 跟进记录删除不等于：
		 *            0-否、1-是("sf.del", 1)
		 * @param 店铺状态等于：
		 *            0-停用、1-启用("shop_state", 1)
		 * @return
		 */
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create().ne("sf.del", 1).eq("shop_state", 1);
		// 根据用户id获取角色
		boolean isDirector = true;
		List<Role> roles = getRoleList();
		for (Role role : roles) {
			if ("运营主管".equals(role.getName())) {
				isDirector = false;
			}
		}
		if (isDirector) {
			wrapper.eq("maid", getUserId());
		}
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iShopFollowService.selectVoByPage(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "跟进记录", ExcelType.XSSF);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, shopFollowVo.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "跟进记录");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

}
